package com.ray.ba.security.api.controller.v1;

import com.ray.ba.security.app.convertor.SecUserConvertor;
import com.ray.ba.security.app.dto.SecUserDto;
import com.ray.ba.security.app.service.SecUserService;
import com.ray.ba.security.infra.entity.SecUser;
import com.ray.ba.starter.web.api.ResponseData;
import com.ray.ba.starter.web.api.ResponsePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * UserController
 *
 * @author ray
 * @date 2020/4/12
 */
@RestController
@RequestMapping("/v1/users")
public class SecUserController {

    @Autowired
    SecUserService secUserService;

    @Autowired
    private SecUserConvertor secUserConvertor;

    @GetMapping
    public ResponseData<ResponsePage<SecUserDto>> list(SecUserDto dto ,
                                                       @RequestParam(required = false, defaultValue = "0") int pageNum,
                                                       @RequestParam(required = false, defaultValue = "10") int pageSize) {
        SecUser secUserDO = secUserConvertor.toEntity(dto);
        List<SecUser> secUserDoList = secUserService.findBy(secUserDO, pageNum, pageSize);
        return ResponseData.success(ResponsePage.restPage(secUserConvertor.toDto(secUserDoList)));
    }

    @PostMapping
    public ResponseData<SecUserDto> create(@Validated @RequestBody SecUserDto dto) {
        SecUser secUserDO = secUserConvertor.toEntity(dto);
        secUserService.insert(secUserDO);
        return  ResponseData.success(secUserConvertor.toDto(secUserDO));
    }

    @PutMapping
    public ResponseData<SecUserDto> update(@Validated(SecUserDto.Update.class) @RequestBody SecUserDto dto) {
        SecUser secUserDO = secUserConvertor.toEntity(dto);
        secUserService.update(secUserDO);
        return  ResponseData.success(secUserConvertor.toDto(secUserDO));
    }

    @DeleteMapping
    public ResponseData delete(@RequestBody Set<Long> ids) {
        secUserService.deleteByIds(ids);
        return  ResponseData.success();
    }



}
